package edu.ustb.aur.utils;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import edu.ustb.aur.moldpackage.*;

public class AbstractMoldJsonDeserializer implements JsonDeserializer<AbstractMold> {
    @Override
    public AbstractMold deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        String moldType = jsonElement.getAsJsonObject().get("type").getAsString();
        switch (moldType) {
            case "CAR_MOLD":
                return jsonDeserializationContext.deserialize(jsonElement, CarMold.class);
            case "HOUSE_MOLD":
                return jsonDeserializationContext.deserialize(jsonElement, HouseMold.class);
            case "PATENT_MOLD":
                return jsonDeserializationContext.deserialize(jsonElement, PatentMold.class);
            case "CURRENCY_MOLD":
                return jsonDeserializationContext.deserialize(jsonElement, CurrencyMold.class);
            default:
                throw new IllegalArgumentException("Error Mold Type!");
        }
    }

}
